package com.swxc.ssm.controller;

import com.swxc.ssm.po.Course;
import com.swxc.ssm.po.User;
import com.swxc.ssm.service.UserService;
import com.swxc.ssm.service.UsertocouService;
import org.apache.tools.ant.taskdefs.email.EmailAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/7/28.
 */

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private  CourseController courseController;

    @Autowired
    private UserService userService;

    @Autowired
    private UsertocouService usertocouService;

    @RequestMapping("/toLogin")
    public ModelAndView toLogin(HttpSession session, String id) {
        session.setAttribute("courseid",id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }



    @RequestMapping("/login")
    public ModelAndView login(HttpSession session, String email, String password) {

        ModelAndView modelAndView = new ModelAndView();
        User user = userService.selectByEmail(email);
        if (!(user.equals(""))&&(password.equals(user.getPassword()))) {
                //将email保存在session，来查看登录状态
                 session.removeAttribute("user");
                 session.setAttribute("user", user);

                 List user_course= usertocouService.selectByUserID(user.getID());

                 session.removeAttribute("user_course");
                 session.setAttribute("user_course",user_course);
               return   courseController.selectAll(session);

        } else {
            modelAndView.setViewName("login");
            return modelAndView;
        }
    }

    @RequestMapping("/toRegister")
    public ModelAndView toRegister() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("register");
        return modelAndView;
    }




    @RequestMapping("/register")
    public ModelAndView register(HttpSession session,String name, String email, String password) {
        ModelAndView modelAndView = new ModelAndView();
        User user = userService.selectByEmail(email);
        if (!(user.equals(""))) {
            modelAndView.setViewName("register");
            return modelAndView;
        } else {

            user.setUUID(UUID.randomUUID().toString());
            user.setName(name);
            user.setEmail(email);
            user.setPassword(password);
            user.setCreatedAt(new Date(System.currentTimeMillis()));
            user.setUpdatedAt(new Date(System.currentTimeMillis()));
            userService.insertSelective(user);

            return login(session,email,password);
        }

    }

    @RequestMapping("/outLogin")
    public ModelAndView outLogin(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();

        session.removeAttribute("user");
        //modelAndView.setViewName("course-index");
        return courseController.selectAll(session);
    }
}
